/**
 * Project Name:springBoot-controller
 * File Name:ValidController.java
 * Package Name:com.libra.controller
 * Date:2018年8月3日下午7:03:20
 * Copyright (c) 2018, wangzhch All Rights Reserved.
 *
*/

package com.libra.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.libra.model.vo.ResponseData;
import com.libra.model.vo.TestValidRequest;
import com.libra.service.TestValidService;
import com.libra.service.exception.BussinessExection;

/**
 * ClassName:ValidController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2018年8月3日 下午7:03:20 <br/>
 * @author   wangzhch
 * @version  
 * @since    JDK 1.8
 * @see 	 
 */
@RestController
@RequestMapping("valid")
public class ValidController {
	private Logger log = LoggerFactory.getLogger(ValidController.class);
	@Autowired
	private TestValidService testValidService;
	
	//ExecutionHandler
	@RequestMapping(value = "/testValid" , method = RequestMethod.POST)
	public ResponseData testValid(@RequestBody  TestValidRequest testValidRequest){
		log.info("请求参数打印：{}",JSON.toJSONString(testValidRequest));
		ResponseData responseData = ResponseData.success();
		try {
			responseData = testValidService.testValid(testValidRequest);
		} catch (BussinessExection e) {
			log.info("自定义异常打印："+e.getMessage());
			responseData = ResponseData.error(e.getMessage());
		}
		log.info("响应结果打印:{}",JSON.toJSONString(responseData));
		return responseData;
	}
	
	@RequestMapping(value = "/testValid2" , method = RequestMethod.POST)
	public ResponseData testValid2(@RequestBody @Valid TestValidRequest testValidRequest , BindingResult result){
		if(result.hasErrors()) {
			log.info("校验错误信息打印:{}",result.getAllErrors().get(0).getDefaultMessage());
			return ResponseData.error(result.getAllErrors().get(0).getDefaultMessage());
		}
		log.info("请求参数打印：{}",JSON.toJSONString(testValidRequest));
		ResponseData responseData = ResponseData.success();
		try {
			responseData = testValidService.testValid(testValidRequest);
		} catch (BussinessExection e) {
			log.info("自定义异常打印："+e.getMessage());
			responseData = ResponseData.error(e.getMessage());
		}
		log.info("响应结果打印:{}",JSON.toJSONString(responseData));
		return responseData;
	}
}

